package com.ruoyi.web.controller.anchor;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.anchor.domain.Live;
import com.ruoyi.anchor.domain.vo.LiveCreateVO;
import com.ruoyi.anchor.service.LiveService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LiveController extends BaseController {


    @Autowired
    private Snowflake snowflake;

    @Autowired
    private LiveService liveService;

    // 创建直播预约
    @PostMapping("/anchor/live/create")
    public AjaxResult create(@RequestBody LiveCreateVO vo) {
        // 获取当前的登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        Live live = new Live();
        BeanUtils.copyProperties(vo, live);
        // 补全信息
        live.setLiveId(snowflake.nextIdStr());
        live.setCreateTime(DateUtils.getNowDate());
        live.setStatus("预约中");
        live.setViewerCount(0L);
        live.setAnchorId(loginUser.getUserId());

        liveService.save(live);

        return success();
    }

}
